package com.esms.esmsadmin.controller.system;

import com.esms.esmsadmin.entity.AdminEntity;
import com.esms.esmsadmin.entity.RespBean;
import com.esms.esmsadmin.entity.RoleEntity;
import com.esms.esmsadmin.service.AdminService;
import com.esms.esmsadmin.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/system/admin")
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    RoleService roleService;
    @GetMapping("/")
    public List<AdminEntity> getAllHr(String keywords){
        return adminService.getAllAdmin(keywords);
    }

    @PutMapping("/")
    public RespBean updateAdmin(@RequestBody AdminEntity admin){
        if(adminService.updateAdmin(admin) == 1){
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    @GetMapping("/roles")
    public List<RoleEntity> getAllRoles(){
        return roleService.getAllRoles();
    }

    @PutMapping("/role")
    public RespBean updateAdminRole( Integer adminId, Integer[] rids){
        if(adminService.updateAdminRole(adminId,rids)){
            return RespBean.ok("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteAdminById(@PathVariable Integer id){
        if(adminService.deleteAdminById(id) == 1){
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }
}
